package com.dllo.lanou.cookapp.adapter;

import android.content.Context;
import android.content.res.Resources;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.bmob.BmobProFile;
import com.bmob.btp.callback.DownloadListener;
import com.dllo.lanou.cookapp.R;
import com.dllo.lanou.cookapp.entity.Post;
import com.dllo.lanou.cookapp.entity.User;
import com.dllo.lanou.cookapp.view.MyListView;
import com.dllo.lanou.cookapp.volly.GetVolleyImage;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.bmob.v3.datatype.BmobFile;

/**
 * Created by xijinhua on 2016/3/16.
 */
public class ShowCookAdapter extends BaseAdapter {
    private Context context;
    private LayoutInflater mInflater;
    private List<Post> list;
    private List<String>  imgs = new ArrayList<>();

    public ShowCookAdapter(Context context){
        this.context = context;

        mInflater = LayoutInflater.from(context);


    }
    public void  addData(List<Post> list){
        if (this.list == null){
           this.list = new ArrayList<>();
        }
        this.list.clear();
        this.list.addAll(list);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return list!= null && list.size()>0 ?list.size():0;
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder = null;
        if (convertView == null){
            convertView =mInflater.inflate(R.layout.item_showcook,null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();

        }

      Post post =   list.get(position);

            Log.i("shouji的数量", position + "");

            if (post.getContent() !=null){
                holder.content.setText(post.getContent());
            }
            if (post.getImgsUrl() != null){
                 String bmobFiles = post.getImgsUrl();
                if (bmobFiles!=null){
                    holder.img1.setVisibility(View.VISIBLE);

                    holder.img1.setAdjustViewBounds(true);
                    GetVolleyImage.getNetWorkImg(bmobFiles, holder.img1);


                }


            }
            User  user = post.getAuthor();
            holder.username.setText(user.getUsername());
            if (user.getImg() != null) {


                Log.i("图片",user.getImg()+"");
                user.getImg().loadImage(context, holder.header);
            }



        return convertView;
    }

    static class ViewHolder{
        @Bind(R.id.id_content)
        TextView content;
        @Bind(R.id.id_header)
        ImageView header;
        @Bind(R.id.id_username)
        TextView username;
        @Bind(R.id.id_img1)
        ImageView img1;

        @Bind(R.id.comment_listview)
        MyListView listView;
        @Bind(R.id.comment_img)
        ImageView comment_img;
        public ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }





}
